<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta http-equiv="Cache-Control" content="no-siteapp">
  <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
  <meta name="renderer" content="webkit">

  
  <title>Android 10.0 Settings 加载流程 | 龙之叶的博客</title>

  <link rel="shortcut icon" href="/images/favicon.png">
  <link rel="alternate" href="/atom.xml" title="龙之叶的博客" type="application/atom+xml">
  <meta name="description" content="Android 10.0 Settings 加载流程">
<meta property="og:type" content="article">
<meta property="og:title" content="Android 10.0 Settings 加载流程">
<meta property="og:url" content="http://longzhiye.top/2023/11/19/2023-11-19/index.html">
<meta property="og:site_name" content="龙之叶的博客">
<meta property="og:description" content="Android 10.0 Settings 加载流程">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2023-11-18T16:00:00.000Z">
<meta property="article:modified_time" content="2024-08-29T02:40:34.913Z">
<meta property="article:author" content="龙之叶">
<meta property="article:tag" content="Android">
<meta property="article:tag" content="Settings">
<meta property="article:tag" content="framework">
<meta name="twitter:card" content="summary">

  <meta name="keywords" content=",Android,Settings,framework">
  <meta name="format-detection" content="telephone=no,email=no">
  <meta name="theme-color" content="#9C27B0">

  <meta name="mobile-web-app-capable" content="yes">
  <meta name="application-name" content="龙之叶的博客">
  <meta name="msapplication-starturl" content="http://longzhiye.top/2023/11/19/2023-11-19/">
  <meta name="msapplication-navbutton-color" content="#9C27B0">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-title" content="龙之叶的博客">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <link rel="apple-touch-icon" href="/images/favicon.png">

  
    <link rel="canonical" href="http://longzhiye.top/2023/11/19/2023-11-19/">
  

  
  

  
  
  

  
<link rel="stylesheet" href="/css/mdui.css">
<link rel="stylesheet" href="/css/style.css">

  
<link rel="stylesheet" href="/custom.css">

<meta name="generator" content="Hexo 5.4.2"></head>

<body class="mdui-appbar-with-toolbar mdui-theme-primary-indigo mdui-theme-accent-pink">
  <script>var a=localStorage.getItem("mdui-theme-layout-dark");if(a){document.getElementsByTagName("body")[0].className+=" mdui-theme-layout-dark"};</script>
  <script>if(window.matchMedia&&window.matchMedia("(prefers-color-scheme: dark)").matches){document.getElementsByTagName("body")[0].className+=" mdui-theme-layout-dark"};</script>
  <script>var a=localStorage.getItem("mdui-drawer-close");if(!a){document.getElementsByTagName("body")[0].className+=" mdui-drawer-body-left"};</script>
  <header id="header" class="mdui-appbar mdui-appbar-fixed mdui-appbar-scroll-hide mdui-appbar-inset">
  <div class="mdui-toolbar mdui-color-theme">
    <a href="javascript:;" class="mdui-btn mdui-btn-icon" mdui-drawer="{target: '#sidebar', swipe: true}"><i class="mdui-icon material-icons">menu</i></a>
    <a href="/" class="mdui-typo-headline">龙之叶的博客</a>
    <div class="mdui-toolbar-spacer"></div>
    <a href="javascript:;" class="mdui-btn mdui-btn-icon" mdui-dialog="{target: '#search'}" mdui-tooltip="{content: '搜索'}"><i class="mdui-icon material-icons">search</i></a>
	<!--
    <a href="/atom.xml" class="mdui-btn mdui-btn-icon" mdui-tooltip="{content: 'RSS'}"><i class="mdui-icon material-icons">rss_feed</i></a>
	-->
  </div>
</header>
<div class="mdui-dialog" id="search">
  
    <div class="search-form">
      <input type="search" class="search-form-input" placeholder="请输入关键字">
    </div>
    <div class="search-result" data-resource="/search.xml"></div>
  
</div>

  <aside id="sidebar" class="mdui-drawer mdui-drawer-full-height">
  <script>var a=localStorage.getItem("mdui-drawer-close");if(a){document.getElementById("sidebar").className+=" mdui-drawer-close"};</script>
  <div class="mdui-grid-tile">
    <img src="/images/banner.png" style="height: 160px;">
    <img src="/images/avatar.png" class="avatar-animation" style="position: absolute; top: 10%; left: 24px; width: 64px; height: 64px; border: 2px solid #fff; border-radius: 50%;background-color:#fff;">
    <div class="mdui-grid-tile-actions">
      <div class="mdui-grid-tile-text">
        <div class="mdui-grid-tile-title">龙之叶</div>
        <div class="mdui-grid-tile-subtitle"><i class="mdui-icon material-icons">art_track</i>生命不息，折腾不止</div>
      </div>
      
        <div class="mdui-grid-tile-buttons">
          <a href="mailto:longzhiye163@163.com" class="mdui-btn mdui-btn-icon" mdui-tooltip="{content: 'longzhiye163@163.com'}"><i class="mdui-icon material-icons">email</i></a>
        </div>
      
    </div>
  </div>

  <div class="mdui-list" mdui-collapse="{accordion: true}">
    <a href="/" class="mdui-list-item mdui-ripple">
      <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-blue">home</i>
      <div class="mdui-list-item-content">主页</div>
    </a>
    <div class="mdui-collapse-item">
      <script>var a=localStorage.getItem("mdui-collapse-item-0");if(a){document.getElementsByClassName("mdui-collapse-item")[0].className+=" mdui-collapse-item-open"};</script>
      <div class="mdui-collapse-item-header mdui-list-item mdui-ripple">
        <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-deep-orange">archive</i>
        <div class="mdui-list-item-content">归档</div>
        <i class="mdui-collapse-item-arrow mdui-icon material-icons">keyboard_arrow_down</i>
      </div>
      <div class="mdui-collapse-item-body mdui-list mdui-list-dense">
        
        <a class="mdui-ripple sidebar_archives-link" href="/archives/2025/07/">七月 2025<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2025/06/">六月 2025<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2025/03/">三月 2025<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2025/02/">二月 2025<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2025/01/">一月 2025<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/10/">十月 2024<span class="mdui-ripple sidebar_archives-count">7</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/09/">九月 2024<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/08/">八月 2024<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/03/">三月 2024<span class="mdui-ripple sidebar_archives-count">5</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/02/">二月 2024<span class="mdui-ripple sidebar_archives-count">11</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/01/">一月 2024<span class="mdui-ripple sidebar_archives-count">9</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/12/">十二月 2023<span class="mdui-ripple sidebar_archives-count">10</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/11/">十一月 2023<span class="mdui-ripple sidebar_archives-count">8</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/10/">十月 2023<span class="mdui-ripple sidebar_archives-count">8</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/09/">九月 2023<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/08/">八月 2023<span class="mdui-ripple sidebar_archives-count">6</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/06/">六月 2023<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/05/">五月 2023<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/04/">四月 2023<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/03/">三月 2023<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/02/">二月 2023<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2022/09/">九月 2022<span class="mdui-ripple sidebar_archives-count">7</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2022/08/">八月 2022<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2017/04/">四月 2017<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2017/03/">三月 2017<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2017/02/">二月 2017<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2017/01/">一月 2017<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2016/12/">十二月 2016<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2016/11/">十一月 2016<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2016/10/">十月 2016<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2016/08/">八月 2016<span class="mdui-ripple sidebar_archives-count">1</span></a>
        
      </div>
    </div>
    <div class="mdui-collapse-item">
      <script>var a=localStorage.getItem("mdui-collapse-item-1");if(a){document.getElementsByClassName("mdui-collapse-item")[1].className+=" mdui-collapse-item-open"};</script>
      <div class="mdui-collapse-item-header mdui-list-item mdui-ripple">
        <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-green">class</i>
        <div class="mdui-list-item-content">分类</div>
        <i class="mdui-collapse-item-arrow mdui-icon material-icons">keyboard_arrow_down</i>
      </div>
      <div class="mdui-collapse-item-body mdui-list mdui-list-dense">
        
        <a class="mdui-ripple sidebar_archives-link" href="/categories/%E6%8A%80%E6%9C%AF/">技术<span class="mdui-ripple sidebar_archives-count">98</span></a><a class="mdui-ripple sidebar_archives-link" href="/categories/%E7%94%9F%E6%B4%BB/">生活<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/categories/%E8%B5%84%E8%AE%AF/">资讯<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/categories/%E9%80%9A%E7%9F%A5/">通知<span class="mdui-ripple sidebar_archives-count">4</span></a>
        
      </div>
    </div>
    <div class="mdui-collapse-item">
      <script>var a=localStorage.getItem("mdui-collapse-item-2");if(a){document.getElementsByClassName("mdui-collapse-item")[2].className+=" mdui-collapse-item-open"};</script>
      <div class="mdui-collapse-item-header mdui-list-item mdui-ripple">
        <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-brown">bookmark</i>
        <div class="mdui-list-item-content">标签</div>
        <i class="mdui-collapse-item-arrow mdui-icon material-icons">keyboard_arrow_down</i>
      </div>
      <div class="mdui-collapse-item-body mdui-list mdui-list-dense">
        
        <a class="mdui-ripple sidebar_archives-link" href="/tags/ANR/" rel="tag">ANR<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/APP/" rel="tag">APP<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Android/" rel="tag">Android<span class="mdui-ripple sidebar_archives-count">89</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Android12/" rel="tag">Android12<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Android13/" rel="tag">Android13<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Android14/" rel="tag">Android14<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/AndroidStudio/" rel="tag">AndroidStudio<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/AutoDraw/" rel="tag">AutoDraw<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Crash/" rel="tag">Crash<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Foxmail/" rel="tag">Foxmail<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Framework/" rel="tag">Framework<span class="mdui-ripple sidebar_archives-count">20</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/GPS%E8%BD%A8%E8%BF%B9%E5%9B%BE/" rel="tag">GPS轨迹图<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Git/" rel="tag">Git<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/GitHub/" rel="tag">GitHub<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Google/" rel="tag">Google<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Hexo/" rel="tag">Hexo<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Java/" rel="tag">Java<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Kernel/" rel="tag">Kernel<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Launcher/" rel="tag">Launcher<span class="mdui-ripple sidebar_archives-count">17</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Lib/" rel="tag">Lib<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Linux/" rel="tag">Linux<span class="mdui-ripple sidebar_archives-count">18</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Markdown/" rel="tag">Markdown<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Material-Design/" rel="tag">Material Design<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/OS-X/" rel="tag">OS X<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/OTA/" rel="tag">OTA<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/QuickTime/" rel="tag">QuickTime<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/SSH/" rel="tag">SSH<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Settings/" rel="tag">Settings<span class="mdui-ripple sidebar_archives-count">6</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/SystemUI/" rel="tag">SystemUI<span class="mdui-ripple sidebar_archives-count">19</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Ubuntu/" rel="tag">Ubuntu<span class="mdui-ripple sidebar_archives-count">7</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/WPS/" rel="tag">WPS<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Wifi/" rel="tag">Wifi<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/WindTerm/" rel="tag">WindTerm<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Windows/" rel="tag">Windows<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/androidmk/" rel="tag">androidmk<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/blog/" rel="tag">blog<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/build/" rel="tag">build<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/feel/" rel="tag">feel<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/framework/" rel="tag">framework<span class="mdui-ripple sidebar_archives-count">50</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/git/" rel="tag">git<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/inotify/" rel="tag">inotify<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/life/" rel="tag">life<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/ninja/" rel="tag">ninja<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/rsync/" rel="tag">rsync<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/standard/" rel="tag">standard<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/test/" rel="tag">test<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E4%BC%81%E4%B8%9A%E5%BE%AE%E4%BF%A1/" rel="tag">企业微信<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/" rel="tag">操作系统<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E6%B5%8B%E8%AF%95/" rel="tag">测试<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E6%BA%90%E7%A0%81/" rel="tag">源码<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E7%94%9F%E6%B4%BB%E6%84%9F%E6%82%9F/" rel="tag">生活感悟<span class="mdui-ripple sidebar_archives-count">1</span></a>
        
      </div>
    </div>
    <a href="/about" class="mdui-list-item mdui-ripple">
      <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-purple">person</i>
      <div class="mdui-list-item-content">关于</div>
    </a>
  </div>

  <div class="mdui-divider"></div>

  <div class="mdui-list" mdui-collapse="{accordion: true}">
    
      <a href="/timeline" class="mdui-list-item mdui-ripple">时间轴</a>
    
    <div class="mdui-collapse-item">
      <script>var a=localStorage.getItem("mdui-collapse-item-3");if(a){document.getElementsByClassName("mdui-collapse-item")[3].className+=" mdui-collapse-item-open"};</script>
      <div class="mdui-collapse-item-header mdui-list-item mdui-ripple">
        <div class="mdui-list-item-content">友情链接</div>
        <i class="mdui-list-item-icon mdui-icon material-icons">link</i>
      </div>
      <div class="mdui-collapse-item-body mdui-list mdui-list-dense">
        
          <a href="http://www.niemingzhao.top" target="_blank" class="mdui-list-item mdui-p-l-2 mdui-text-color-theme-accent mdui-ripple" style="justify-content: center;">聂明照的博客</a>
        
        
      </div>
    </div>
  </div>
</aside>

  <main id="main" class="mdui-m-t-5 fadeIn animated">
  
<link rel="stylesheet" href="//cdn.bootcdn.net/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.css">

  <style>#main article .mdui-card-content .center-block{display:block!important;margin-right:auto!important;margin-left:auto!important}</style>
  <style>#main article .mdui-card-content .text-center{text-align:center!important}</style>
  <article class="mdui-card mdui-m-b-5">
    <header class="mdui-card-media">
      <img src="/images/random/material-15.png" style="max-height: 240px;">
      <div class="mdui-card-media-covered">
        <div class="mdui-card-primary">
          <div class="mdui-card-primary-title">Android 10.0 Settings 加载流程</div>
          <div class="mdui-card-primary-subtitle"><i class="iconfont">&#xe697;</i> 2023-11-19 / <i class="iconfont">&#xe601;</i> 龙之叶 &nbsp;&nbsp; <span id="busuanzi_container_page_pv" style="display: none;"><i class="iconfont">&#xe7fd;</i> <span id="busuanzi_value_page_pv"></span></span></div>
        </div>
      </div>
      <div class="mdui-card-menu">
        
          <button class="mdui-btn mdui-btn-icon mdui-text-color-white" mdui-menu="{target: '#qrcode', align: 'right'}"><i class="mdui-icon material-icons">devices</i></button>
          <ul class="mdui-menu" id="qrcode">
            
              <li class="mdui-menu-item"><a class="mdui-text-center mdui-ripple">发送到手机</a></li>
            
            <li class="mdui-menu-item" disabled>
              
                <img src="//qr.liantu.com/api.php?w=246&m=10&text=http://longzhiye.top/2023/11/19/2023-11-19/">
              
            </li>
          </ul>
        
        
          <button class="mdui-btn mdui-btn-icon mdui-text-color-white" mdui-menu="{target: '#share_menu', align: 'right'}"><i class="mdui-icon material-icons">share</i></button>
          <ul class="mdui-menu" id="share_menu">
            <li class="mdui-menu-item">
              <a href="https://service.weibo.com/share/share.php?appkey=&title=Android 10.0 Settings 加载流程&url=http://longzhiye.top/2023/11/19/2023-11-19/&pic=http://longzhiye.top/images/favicon.png&searchPic=false&style=simple" target="_blank" class="mdui-ripple">分享到 Weibo</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://twitter.com/intent/tweet?text=Android 10.0 Settings 加载流程&url=http://longzhiye.top/2023/11/19/2023-11-19/&via=龙之叶" target="_blank" class="mdui-ripple">分享到 Twitter</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://www.facebook.com/sharer/sharer.php?u=http://longzhiye.top/2023/11/19/2023-11-19/" target="_blank" class="mdui-ripple">分享到 Facebook</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://plus.google.com/share?url=http://longzhiye.top/2023/11/19/2023-11-19/" target="_blank" class="mdui-ripple">分享到 Google+</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://www.linkedin.com/shareArticle?mini=true&url=http://longzhiye.top/2023/11/19/2023-11-19/&title=Android 10.0 Settings 加载流程" target="_blank" class="mdui-ripple">分享到 LinkedIn</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://connect.qq.com/widget/shareqq/index.html?site=龙之叶的博客&title=Android 10.0 Settings 加载流程&pics=http://longzhiye.top/images/favicon.png&url=http://longzhiye.top/2023/11/19/2023-11-19/" target="_blank" class="mdui-ripple">分享到 QQ</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://telegram.me/share/url?url=http://longzhiye.top/2023/11/19/2023-11-19/&text=Android 10.0 Settings 加载流程" target="_blank" class="mdui-ripple">分享到 Telegram</a>
            </li>
          </ul>
        
      </div>
    </header>
    <div class="mdui-card-content mdui-typo">
      <h3 id="一、系统设置首页"><a href="#一、系统设置首页" class="headerlink" title="一、系统设置首页"></a>一、系统设置首页</h3><p>代码路径：packages/app/Settings/</p>
<h4 id="1-主界面加载："><a href="#1-主界面加载：" class="headerlink" title="1 主界面加载："></a>1 主界面加载：</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- Alias <span class="keyword">for</span> launcher activity only, as <span class="built_in">this</span> belongs to each profile. --&gt;</span><br><span class="line">    &lt;activity-alias android:name=<span class="string">&quot;Settings&quot;</span></span><br><span class="line">            android:label=<span class="string">&quot;@string/settings_label_launcher&quot;</span></span><br><span class="line">            android:launchMode=<span class="string">&quot;singleTask&quot;</span></span><br><span class="line">            android:targetActivity=<span class="string">&quot;.homepage.SettingsHomepageActivity&quot;</span>&gt;</span><br><span class="line">        &lt;intent-filter&gt;</span><br><span class="line">            &lt;action android:name=<span class="string">&quot;android.intent.action.MAIN&quot;</span> /&gt;</span><br><span class="line">            &lt;category android:name=<span class="string">&quot;android.intent.category.DEFAULT&quot;</span> /&gt;</span><br><span class="line">            &lt;category android:name=<span class="string">&quot;android.intent.category.LAUNCHER&quot;</span> /&gt;</span><br><span class="line">        &lt;/intent-filter&gt;</span><br><span class="line">        &lt;meta-data android:name=<span class="string">&quot;android.app.shortcuts&quot;</span> android:resource=<span class="string">&quot;@xml/shortcuts&quot;</span>/&gt;</span><br><span class="line">    &lt;/activity-alias&gt;</span><br></pre></td></tr></table></figure>
<p>Settings的主界面是Settings.java，但是从Settings.java来看，除了大量的静态类继承SettingsActivity，就无其他有效信息了。但看其xml定义可以发现targetActivity属性，实质应是SettingsHomepageActivity.java。<br>先看其xml配置:</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">activity</span> <span class="attr">android:name</span>=<span class="string">&quot;.homepage.SettingsHomepageActivity&quot;</span></span></span><br><span class="line"><span class="tag">          <span class="attr">android:label</span>=<span class="string">&quot;@string/settings_label_launcher&quot;</span></span></span><br><span class="line"><span class="tag">          <span class="attr">android:theme</span>=<span class="string">&quot;@style/Theme.Settings.Home&quot;</span></span></span><br><span class="line"><span class="tag">          <span class="attr">android:launchMode</span>=<span class="string">&quot;singleTask&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">intent-filter</span> <span class="attr">android:priority</span>=<span class="string">&quot;1&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">action</span> <span class="attr">android:name</span>=<span class="string">&quot;android.settings.SETTINGS&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">category</span> <span class="attr">android:name</span>=<span class="string">&quot;android.intent.category.DEFAULT&quot;</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">intent-filter</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta-data</span> <span class="attr">android:name</span>=<span class="string">&quot;com.android.settings.PRIMARY_PROFILE_CONTROLLED&quot;</span></span></span><br><span class="line"><span class="tag">               <span class="attr">android:value</span>=<span class="string">&quot;true&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">activity</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>SettingsHomepageActivity.java，主要从onCreate()方法开始:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title function_">onCreate</span><span class="params">(Bundle savedInstanceState)</span> &#123;</span><br><span class="line">    <span class="built_in">super</span>.onCreate(savedInstanceState);</span><br><span class="line">    setContentView(R.layout.settings_homepage_container);</span><br><span class="line">    <span class="keyword">final</span> <span class="type">View</span> <span class="variable">root</span> <span class="operator">=</span> findViewById(R.id.settings_homepage_container);</span><br><span class="line">    root.setSystemUiVisibility(</span><br><span class="line">            View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);</span><br><span class="line">    setHomepageContainerPaddingTop();</span><br><span class="line">    <span class="keyword">final</span> <span class="type">Toolbar</span> <span class="variable">toolbar</span> <span class="operator">=</span> findViewById(R.id.search_action_bar);</span><br><span class="line">    FeatureFactory.getFactory(<span class="built_in">this</span>).getSearchFeatureProvider()</span><br><span class="line">            .initSearchToolbar(<span class="built_in">this</span> <span class="comment">/* activity */</span>, toolbar, SettingsEnums.SETTINGS_HOMEPAGE);</span><br><span class="line">    <span class="keyword">final</span> <span class="type">ImageView</span> <span class="variable">avatarView</span> <span class="operator">=</span> findViewById(R.id.account_avatar);</span><br><span class="line">    <span class="keyword">final</span> <span class="type">AvatarViewMixin</span> <span class="variable">avatarViewMixin</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">AvatarViewMixin</span>(<span class="built_in">this</span>, avatarView);</span><br><span class="line">    getLifecycle().addObserver(avatarViewMixin);</span><br><span class="line">    <span class="keyword">if</span> (!getSystemService(ActivityManager.class).isLowRamDevice()) &#123;</span><br><span class="line">        <span class="comment">// Only allow contextual feature on high ram devices.</span></span><br><span class="line">        showFragment(<span class="keyword">new</span> <span class="title class_">ContextualCardsFragment</span>(), R.id.contextual_cards_content);</span><br><span class="line">    &#125;</span><br><span class="line">    showFragment(<span class="keyword">new</span> <span class="title class_">TopLevelSettings</span>(), R.id.main_content);</span><br><span class="line">    ((FrameLayout) findViewById(R.id.main_content))</span><br><span class="line">            .getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>可以看到主界面的layout为settings_homepage_container.xml:</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">androidx.coordinatorlayout.widget.CoordinatorLayout</span></span></span><br><span class="line"><span class="tag">    <span class="attr">xmlns:android</span>=<span class="string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">xmlns:app</span>=<span class="string">&quot;http://schemas.android.com/apk/res-auto&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:id</span>=<span class="string">&quot;@+id/settings_homepage_container&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:fitsSystemWindows</span>=<span class="string">&quot;true&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:layout_width</span>=<span class="string">&quot;match_parent&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:layout_height</span>=<span class="string">&quot;match_parent&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">androidx.core.widget.NestedScrollView</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:id</span>=<span class="string">&quot;@+id/main_content_scrollable_container&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:layout_width</span>=<span class="string">&quot;match_parent&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:layout_height</span>=<span class="string">&quot;match_parent&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">app:layout_behavior</span>=<span class="string">&quot;com.android.settings.widget.FloatingAppBarScrollingViewBehavior&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">LinearLayout</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:id</span>=<span class="string">&quot;@+id/homepage_container&quot;</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:layout_width</span>=<span class="string">&quot;match_parent&quot;</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:layout_height</span>=<span class="string">&quot;wrap_content&quot;</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:orientation</span>=<span class="string">&quot;vertical&quot;</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:descendantFocusability</span>=<span class="string">&quot;blocksDescendants&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">FrameLayout</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:id</span>=<span class="string">&quot;@+id/contextual_cards_content&quot;</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:layout_width</span>=<span class="string">&quot;match_parent&quot;</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:layout_height</span>=<span class="string">&quot;wrap_content&quot;</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:layout_marginStart</span>=<span class="string">&quot;@dimen/contextual_card_side_margin&quot;</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:layout_marginEnd</span>=<span class="string">&quot;@dimen/contextual_card_side_margin&quot;</span>/&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">FrameLayout</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:id</span>=<span class="string">&quot;@+id/main_content&quot;</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:layout_width</span>=<span class="string">&quot;match_parent&quot;</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:layout_height</span>=<span class="string">&quot;wrap_content&quot;</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:animateLayoutChanges</span>=<span class="string">&quot;true&quot;</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:background</span>=<span class="string">&quot;?android:attr/windowBackground&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">LinearLayout</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">androidx.core.widget.NestedScrollView</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">com.google.android.material.appbar.AppBarLayout</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:layout_width</span>=<span class="string">&quot;match_parent&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:layout_height</span>=<span class="string">&quot;wrap_content&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">include</span> <span class="attr">layout</span>=<span class="string">&quot;@layout/search_bar&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">com.google.android.material.appbar.AppBarLayout</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">androidx.coordinatorlayout.widget.CoordinatorLayout</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>主界面布局中主要包含三部分：两个FrameLayout，一个顶部快捷搜索栏。其中Id为main_content的FrameLayout就是用来显示主设置内容的，即Settings的一级菜单项界面。.homepage.SettingsHomepageActivity 中的逻辑并不复杂，直接加载了TopLevelSettings这个Fragment。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">showFragment(<span class="keyword">new</span> <span class="title class_">TopLevelSettings</span>(), R.id.main_content);</span><br></pre></td></tr></table></figure>
<p>TopLevelSettings通过AndroidX的Preference来展示设置项列表，设置项列表的内容通过静态配置+动态添加的方式获取。<br>后面分开分析：SettingsActivity.java、DashboardFragment.java。</p>
<h4 id="2-SettingsActivity-java"><a href="#2-SettingsActivity-java" class="headerlink" title="2 SettingsActivity.java"></a>2 SettingsActivity.java</h4><p>Settings 继承了 SettingsActivity，有着大量的静态类，但其中并没有实现任何逻辑，那它是怎么加载到自己应有的布局的呢?<br>其实这些Activity的逻辑都是在SettingsActivity中实现。<br>在父类SettingsActivity的onCreate()中：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">   <span class="meta">@Override</span>  </span><br><span class="line">   <span class="keyword">protected</span> <span class="keyword">void</span> <span class="title function_">onCreate</span><span class="params">(Bundle savedState)</span> &#123;  </span><br><span class="line">       <span class="built_in">super</span>.onCreate(savedState);  </span><br><span class="line">       <span class="type">long</span> <span class="variable">startTime</span> <span class="operator">=</span> System.currentTimeMillis();  </span><br><span class="line">       <span class="comment">//工厂类实现方法com.android.settings.overlay.FeatureFactoryImpl.java  </span></span><br><span class="line">       <span class="keyword">final</span> <span class="type">FeatureFactory</span> <span class="variable">factory</span> <span class="operator">=</span> FeatureFactory.getFactory(<span class="built_in">this</span>);  </span><br><span class="line">       <span class="comment">//获取菜单信息的工厂类，实现类为DashboardFeatureProviderImpl.java  </span></span><br><span class="line">       mDashboardFeatureProvider = factory.getDashboardFeatureProvider(<span class="built_in">this</span>);  </span><br><span class="line">       mMetricsFeatureProvider = factory.getMetricsFeatureProvider();  </span><br><span class="line"><span class="comment">// 第一步    从intent信息中获取&lt;meta-data/&gt;标签名为&quot;com.android.settings.FRAGMENT_CLASS&quot;的值（下文用于加载Fragment的类名）  </span></span><br><span class="line">       getMetaData();  </span><br><span class="line"><span class="comment">// 第二步</span></span><br><span class="line">     <span class="keyword">final</span> <span class="type">Intent</span> <span class="variable">intent</span> <span class="operator">=</span> getIntent();</span><br><span class="line">     <span class="keyword">if</span> (intent.hasExtra(EXTRA_UI_OPTIONS)) &#123;</span><br><span class="line">         getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, <span class="number">0</span>));</span><br><span class="line">     &#125;</span><br><span class="line">     </span><br><span class="line">       <span class="comment">//获取上面getMetaData()得到的类名  </span></span><br><span class="line">       <span class="keyword">final</span> <span class="type">String</span> <span class="variable">initialFragmentName</span> <span class="operator">=</span> intent.getStringExtra(EXTRA_SHOW_FRAGMENT);  </span><br><span class="line">       <span class="comment">//是否为快捷进入方式（如从其它的应用进入Settings的某个设置项）  </span></span><br><span class="line">       mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||  </span><br><span class="line">               intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, <span class="literal">false</span>);  </span><br><span class="line">       ... ...  </span><br><span class="line">    </span><br><span class="line">       <span class="keyword">if</span> (savedState != <span class="literal">null</span>) &#123;  </span><br><span class="line">         ... ...  </span><br><span class="line">       &#125; <span class="keyword">else</span> &#123;  </span><br><span class="line"><span class="comment">//  第三步   加载布局  </span></span><br><span class="line">           launchSettingFragment(initialFragmentName, isSubSettings, intent);  </span><br><span class="line">       &#125;  </span><br><span class="line"> </span><br><span class="line">       ... ...  </span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>
<p>第一步：<br>首先通过getMetaData()获取该Activity在manifest中配置的fragment, 并赋值给mFragmentClass。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title function_">getMetaData</span><span class="params">()</span> &#123;</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        <span class="type">ActivityInfo</span> <span class="variable">ai</span> <span class="operator">=</span> getPackageManager().getActivityInfo(getComponentName(),</span><br><span class="line">                PackageManager.GET_META_DATA);</span><br><span class="line">        <span class="keyword">if</span> (ai == <span class="literal">null</span> || ai.metaData == <span class="literal">null</span>) <span class="keyword">return</span>;</span><br><span class="line">        mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);</span><br><span class="line">    &#125; <span class="keyword">catch</span> (NameNotFoundException nnfe) &#123;</span><br><span class="line">        <span class="comment">// No recovery</span></span><br><span class="line">        Log.d(LOG_TAG, <span class="string">&quot;Cannot get Metadata for: &quot;</span> + getComponentName().toString());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>第二步：<br>通过getIntent()方法、getStartingFragmentClass()方法筛选出要启动的Fragment。<br>第三步：<br>通过launchSettingFragment()启动对应Fragment，这里的initialFragmentName参数就是第二步Intent中包含的EXTRA_SHOW_FRAGMENT参数，mFragmentClass不为空的情况下传入的就是mFragmentClass。</p>
<h4 id="3-DashboardFragment-java"><a href="#3-DashboardFragment-java" class="headerlink" title="3 DashboardFragment.java"></a>3 DashboardFragment.java</h4><p>通过上面知道，SettingsHomepageActivity 直接加载了TopLevelSettings这个Fragment。而该Fragment继承了DashboardFragment，先来看TopLevelSettings的构造方法：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="title function_">TopLevelSettings</span><span class="params">()</span> &#123;</span><br><span class="line">    <span class="keyword">final</span> <span class="type">Bundle</span> <span class="variable">args</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Bundle</span>();</span><br><span class="line">    <span class="comment">// Disable the search icon because this page uses a full search view in actionbar.</span></span><br><span class="line">    args.putBoolean(NEED_SEARCH_ICON_IN_ACTION_BAR, <span class="literal">false</span>);</span><br><span class="line">    setArguments(args);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>可以看到构造方法中仅设置了个标志位，再根据framgments生命周期先来看onAttach()方法:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">onAttach</span><span class="params">(Context context)</span> &#123;</span><br><span class="line">    <span class="built_in">super</span>.onAttach(context);</span><br><span class="line">    use(SupportPreferenceController.class).setActivity(getActivity());</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>调用父类DashboardFragment.java的onAttach()方法，此方法主要是完成mPreferenceControllers的加载。<br>接着看onCreate()方法，因为TopLevelSettings未重写父类的方法，所以直接看父类DashboardFragment的onCreate()方法。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">onCreate</span><span class="params">(Bundle icicle)</span> &#123;</span><br><span class="line">    <span class="built_in">super</span>.onCreate(icicle);</span><br><span class="line">    <span class="comment">// Set ComparisonCallback so we get better animation when list changes.</span></span><br><span class="line">    getPreferenceManager().setPreferenceComparisonCallback(</span><br><span class="line">            <span class="keyword">new</span> <span class="title class_">PreferenceManager</span>.SimplePreferenceComparisonCallback());</span><br><span class="line">    <span class="keyword">if</span> (icicle != <span class="literal">null</span>) &#123;</span><br><span class="line">        <span class="comment">// Upon rotation configuration change we need to update preference states before any</span></span><br><span class="line">        <span class="comment">// editing dialog is recreated (that would happen before onResume is called).</span></span><br><span class="line">        updatePreferenceStates();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>根据log定位发现，其后调用DashboardFragment.java的onCreatePreferences()方法：这里我也不知道怎么调用到这来的，哈哈。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">onCreatePreferences</span><span class="params">(Bundle savedInstanceState, String rootKey)</span> &#123;</span><br><span class="line">    refreshAllPreferences(getLogTag());</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Refresh all preference items, including both static prefs from xml, and dynamic items from</span></span><br><span class="line"><span class="comment"> * DashboardCategory.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title function_">refreshAllPreferences</span><span class="params">(<span class="keyword">final</span> String TAG)</span> &#123;</span><br><span class="line">    <span class="keyword">final</span> <span class="type">PreferenceScreen</span> <span class="variable">screen</span> <span class="operator">=</span> getPreferenceScreen();</span><br><span class="line">    <span class="comment">// First remove old preferences.</span></span><br><span class="line">    <span class="keyword">if</span> (screen != <span class="literal">null</span>) &#123;</span><br><span class="line">        <span class="comment">// Intentionally do not cache PreferenceScreen because it will be recreated later.</span></span><br><span class="line">        screen.removeAll();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// Add resource based tiles.</span></span><br><span class="line">    displayResourceTiles();</span><br><span class="line">    refreshDashboardTiles(TAG);</span><br><span class="line">    <span class="keyword">final</span> <span class="type">Activity</span> <span class="variable">activity</span> <span class="operator">=</span> getActivity();</span><br><span class="line">    <span class="keyword">if</span> (activity != <span class="literal">null</span>) &#123;</span><br><span class="line">        Log.d(TAG, <span class="string">&quot;All preferences added, reporting fully drawn&quot;</span>);</span><br><span class="line">        activity.reportFullyDrawn();</span><br><span class="line">    &#125;</span><br><span class="line">    updatePreferenceVisibility(mPreferenceControllers);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>以看到此方法主要是用来加载显示的preference items，主要分为两部分，一个是静态xml定义的prefs(调用displayResourceTiles()方法)，另一部分是从DashboardCategory动态加载(调用refreshDashboardTiles(TAG)方法，其中TAG为 “TopLevelSettings”)。<br>displayResourceTiles()<br>此方法主要是从xml资源文件中加载显示prefs：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Displays resource based tiles.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title function_">displayResourceTiles</span><span class="params">()</span> &#123;</span><br><span class="line">    <span class="keyword">final</span> <span class="type">int</span> <span class="variable">resId</span> <span class="operator">=</span> getPreferenceScreenResId();</span><br><span class="line">    <span class="keyword">if</span> (resId &lt;= <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    addPreferencesFromResource(resId);</span><br><span class="line">    <span class="keyword">final</span> <span class="type">PreferenceScreen</span> <span class="variable">screen</span> <span class="operator">=</span> getPreferenceScreen();</span><br><span class="line">    screen.setOnExpandButtonClickListener(<span class="built_in">this</span>);</span><br><span class="line">    mPreferenceControllers.values().stream().flatMap(Collection::stream).forEach(</span><br><span class="line">            controller -&gt; controller.displayPreference(screen));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>首先调用getPreferenceScreenResId()方法获取所要加载的xml的ID：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="keyword">protected</span> <span class="keyword">abstract</span> <span class="type">int</span> <span class="title function_">getPreferenceScreenResId</span><span class="params">()</span>;</span><br></pre></td></tr></table></figure>
<p>最终回调用到子类TopLevelSettings.java的getPreferenceScreenResId()方法：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="keyword">protected</span> <span class="type">int</span> <span class="title function_">getPreferenceScreenResId</span><span class="params">()</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> R.xml.top_level_settings;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>此主要是调用androidX Preference的addPreferencesFromResource()方法。此方法主要是将preferenceScreen下所有Preference添加到ArrayList中，然后再根据此集合构建生成PreferenceGroupAdapter，最后将此adapter设置到listview中，完成数据绑定，从而完成界面加载。在这里就要明白mPreferenceControllers是什么，在哪初始化的？<br>我们很快就可以找到：在onAttach()中添加的。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">final</span> List&lt;AbstractPreferenceController&gt; controllers = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line"><span class="comment">// Load preference controllers from code</span></span><br><span class="line"><span class="keyword">final</span> List&lt;AbstractPreferenceController&gt; controllersFromCode =</span><br><span class="line">        createPreferenceControllers(context);</span><br><span class="line"><span class="comment">// Load preference controllers from xml definition</span></span><br><span class="line"><span class="keyword">final</span> List&lt;BasePreferenceController&gt; controllersFromXml = PreferenceControllerListHelper</span><br><span class="line">        .getPreferenceControllersFromXml(context, getPreferenceScreenResId());</span><br><span class="line"><span class="comment">// Filter xml-based controllers in case a similar controller is created from code already.</span></span><br><span class="line"><span class="keyword">final</span> List&lt;BasePreferenceController&gt; uniqueControllerFromXml =</span><br><span class="line">        PreferenceControllerListHelper.filterControllers(</span><br><span class="line">                controllersFromXml, controllersFromCode);</span><br><span class="line"><span class="comment">// Add unique controllers to list.</span></span><br><span class="line"><span class="keyword">if</span> (controllersFromCode != <span class="literal">null</span>) &#123;</span><br><span class="line">    controllers.addAll(controllersFromCode);</span><br><span class="line">&#125;</span><br><span class="line">controllers.addAll(uniqueControllerFromXml);</span><br><span class="line"><span class="comment">// And wire up with lifecycle.</span></span><br><span class="line"><span class="keyword">final</span> <span class="type">Lifecycle</span> <span class="variable">lifecycle</span> <span class="operator">=</span> getSettingsLifecycle();</span><br><span class="line">uniqueControllerFromXml</span><br><span class="line">        .stream()</span><br><span class="line">        .filter(controller -&gt; controller <span class="keyword">instanceof</span> LifecycleObserver)</span><br><span class="line">        .forEach(</span><br><span class="line">                controller -&gt; lifecycle.addObserver((LifecycleObserver) controller));</span><br><span class="line">mPlaceholderPreferenceController =</span><br><span class="line">        <span class="keyword">new</span> <span class="title class_">DashboardTilePlaceholderPreferenceController</span>(context);</span><br><span class="line">controllers.add(mPlaceholderPreferenceController);</span><br><span class="line"><span class="keyword">for</span> (AbstractPreferenceController controller : controllers) &#123;</span><br><span class="line">    addPreferenceController(controller);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>可以发现：</p>
<ol>
<li>从代码中加载preference controllers，调用createPreferenceControllers()方法；</li>
<li>从xml定义中加载preference controllers，调用getPreferenceControllersFromXml()方法。</li>
<li>过滤重复定义的controller等，赋值填充mPreferenceControllers。</li>
</ol>
<p>再回到displayResourceTiles()方法中的：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">mPreferenceControllers.values().stream().flatMap(Collection::stream).forEach(</span><br><span class="line">                controller -&gt; controller.displayPreference(screen));</span><br></pre></td></tr></table></figure>
<p>此语句主要就是调用各个controller的displayPreference()方法。<br>以网络和互联网菜单项为例，xml中配置的controller为”com.android.settings.network.TopLevelNetworkEntryPreferenceController”，查看TopLevelNetworkEntryPreferenceController.java发现，其内并未实现displayPreference()方法，查看继承关系：是继承BasePreferenceController的，接着查看BasePreferenceController中的displayPreference()方法。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Displays preference in this controller.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">displayPreference</span><span class="params">(PreferenceScreen screen)</span> &#123;</span><br><span class="line">    <span class="built_in">super</span>.displayPreference(screen);</span><br><span class="line">    <span class="keyword">if</span> (getAvailabilityStatus() == DISABLED_DEPENDENT_SETTING) &#123;</span><br><span class="line">        <span class="comment">// Disable preference if it depends on another setting.</span></span><br><span class="line">        <span class="keyword">final</span> <span class="type">Preference</span> <span class="variable">preference</span> <span class="operator">=</span> screen.findPreference(getPreferenceKey());</span><br><span class="line">        <span class="keyword">if</span> (preference != <span class="literal">null</span>) &#123;</span><br><span class="line">            preference.setEnabled(<span class="literal">false</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>又是调用BasePreferenceController父类AbstractPreferenceController中的displayPreference：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Displays preference in this controller.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">displayPreference</span><span class="params">(PreferenceScreen screen)</span> &#123;</span><br><span class="line">    <span class="keyword">final</span> <span class="type">String</span> <span class="variable">prefKey</span> <span class="operator">=</span> getPreferenceKey();</span><br><span class="line">    <span class="keyword">if</span> (TextUtils.isEmpty(prefKey)) &#123;</span><br><span class="line">        Log.w(TAG, <span class="string">&quot;Skipping displayPreference because key is empty:&quot;</span> + getClass().getName());</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (isAvailable()) &#123;</span><br><span class="line">        setVisible(screen, prefKey, <span class="literal">true</span> <span class="comment">/* visible */</span>);</span><br><span class="line">        <span class="keyword">if</span> (<span class="built_in">this</span> <span class="keyword">instanceof</span> Preference.OnPreferenceChangeListener) &#123;</span><br><span class="line">            <span class="keyword">final</span> <span class="type">Preference</span> <span class="variable">preference</span> <span class="operator">=</span> screen.findPreference(prefKey);</span><br><span class="line">            preference.setOnPreferenceChangeListener(</span><br><span class="line">                    (Preference.OnPreferenceChangeListener) <span class="built_in">this</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        setVisible(screen, prefKey, <span class="literal">false</span> <span class="comment">/* visible */</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ol>
<li>getPreferenceKey()获取preference的key，会调用到子类BasePreferenceController.java的getPreferenceKey()方法：<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="keyword">public</span> String <span class="title function_">getPreferenceKey</span><span class="params">()</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> mPreferenceKey;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
而据上面分析到mPreferenceKey实质上即为xml中每个preference配置的android:key属性的值，即此处应为”top_level_network”。（以网络和互联网菜单项为例）</li>
<li>isAvailable()；判断此preference是否可用即是否应该被显示。如果返回true，则被显示出来，反之则不被显示，最终也会调用到BasePreferenceController.java的isAvailable()方法：<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">final</span> <span class="type">boolean</span> <span class="title function_">isAvailable</span><span class="params">()</span> &#123;</span><br><span class="line">    <span class="keyword">final</span> <span class="type">int</span> <span class="variable">availabilityStatus</span> <span class="operator">=</span> getAvailabilityStatus();</span><br><span class="line">    <span class="keyword">return</span> (availabilityStatus == AVAILABLE</span><br><span class="line">            || availabilityStatus == AVAILABLE_UNSEARCHABLE</span><br><span class="line">            || availabilityStatus == DISABLED_DEPENDENT_SETTING);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ol>
<p><strong>注意：看这里的BasePreferenceController.java中的isAvailable()方法中的getAvailabilityStatus()，一直跟进去，会发现调用的是：BasePreferenceController子类TopLevelNetworkEntryPreferenceController.java的getAvailabilityStatus()方法：</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getAvailabilityStatus</span><span class="params">()</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> Utils.isDemoUser(mContext) ? UNSUPPORTED_ON_DEVICE : AVAILABLE_UNSEARCHABLE;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ol start="3">
<li>调用setVisible()方法设置是否可被显示：setVisible(screen, prefKey, true /* visible */);</li>
</ol>
<p><strong>frameworks/base/packages/SettingsLib/src/com/android/settingslib/core/AbstractPreferenceController.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">protected</span> <span class="keyword">final</span> <span class="keyword">void</span> <span class="title function_">setVisible</span><span class="params">(PreferenceGroup group, String key, <span class="type">boolean</span> isVisible)</span> &#123;</span><br><span class="line">    <span class="keyword">final</span> <span class="type">Preference</span> <span class="variable">pref</span> <span class="operator">=</span> group.findPreference(key);</span><br><span class="line">    <span class="keyword">if</span> (pref != <span class="literal">null</span>) &#123;</span><br><span class="line">        pref.setVisible(isVisible);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ol start="4">
<li>判断controller是否实现了Preference.OnPreferenceChangeListener接口，是，则设置监听。<br>综上，如果希望preference不被显示在界面上，可以通过实现相关preference的controller的getAvailabilityStatus()方法，使此方法的返回值不为AVAILABLE、AVAILABLE_UNSEARCHABLE、DISABLED_DEPENDENT_SETTING即可。</li>
<li>继续看查看BasePreferenceController.java的displayPreference()方法的剩余语句：<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (getAvailabilityStatus() == DISABLED_DEPENDENT_SETTING) &#123;</span><br><span class="line">    <span class="comment">// Disable preference if it depends on another setting.</span></span><br><span class="line">    <span class="keyword">final</span> <span class="type">Preference</span> <span class="variable">preference</span> <span class="operator">=</span> screen.findPreference(getPreferenceKey());</span><br><span class="line">    <span class="keyword">if</span> (preference != <span class="literal">null</span>) &#123;</span><br><span class="line">        preference.setEnabled(<span class="literal">false</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
根据子类controller实现的getAvailabilityStatus()方法的返回值判断是否需要将此preference置为不可点击。<br>至此，DashboardFragment.java中displayResourceTiles()方法分析完成。</li>
</ol>
<h3 id="总结："><a href="#总结：" class="headerlink" title="总结："></a>总结：</h3><ol>
<li>Settings的主Activity实质实现是在SettingsHomepageActivity.java内；</li>
<li>Settings的主界面设置item的显示是在fragment上，fragment为TopLevelSettings.java，加载显示的布局为top_level_settings.xml；</li>
<li>Settings主界面设置项item的加载显示主要分为两部分，一部分是xml定义的静态加载，xml为top_level_settings.xml；一部分是DashboardCategory来获取动态加载。</li>
<li>每个设置项item均为一个preference，通过xml定义加载时，必须要有一个controller，可以是在xml中定义”settings:controller”属性声明，名称必须与类的包名路径相同；也可直接在相关fragment中实现createPreferenceControllers()方法去调用构造相关controller。此二者存其一即可。</li>
<li>xml中配置preference时，必须定义”android:key“属性；</li>
<li>需要隐藏不显示某个设置项时，一是可以直接在xml中注释其定义；二是可以在相关设置项preference的controller类中实现getAvailabilityStatus()方法，使此方法的返回值不为AVAILABLE、AVAILABLE_UNSEARCHABLE、DISABLED_DEPENDENT_SETTING即可；</li>
<li>如果需要某个设置项不可点击，一是可以直接调用setEnabled()。二是可以在相关设置项preference的controller类中实现getAvailabilityStatus()方法，使此方法的返回值为DISABLED_DEPENDENT_SETTING即可。</li>
</ol>

      
      <blockquote>
        
        <strong>本文链接：</strong><br><a href="http://longzhiye.top/2023/11/19/2023-11-19/">http://longzhiye.top/2023/11/19/2023-11-19/</a>
      </blockquote>
    </div>
    <footer class="mdui-card-actions">
      
        <a class="mdui-ripple article_categories-link" href="/categories/%E6%8A%80%E6%9C%AF/">技术</a>
      
      
        <a class="mdui-ripple article_tags-link" href="/tags/Android/" rel="tag">Android</a><a class="mdui-ripple article_tags-link" href="/tags/Settings/" rel="tag">Settings</a><a class="mdui-ripple article_tags-link" href="/tags/framework/" rel="tag">framework</a>
      
    </footer>
    
  </article>
  
<script src="//cdn.bootcdn.net/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="//cdn.bootcdn.net/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.js"></script>

  <script>$("#main article .mdui-card-content img.fancybox").on("click",function(e){$.fancybox.open({src:$(this).attr("src")});});</script>


  <nav id="paginator">
    
      <a rel="prev" class="extend prev" href="/2023/11/25/2023-11-25/">
        <button aria-label="prev" class="mdui-btn mdui-btn-raised mdui-btn-icon mdui-color-theme-accent mdui-ripple"><i class="mdui-icon material-icons">arrow_back</i></button>
        <span class="mdui-p-x-3" mdui-tooltip="{content: 'Android 10.0 Launcher 启动流程'}">上一篇</span>
      </a>
    
    <div class="spacer"></div>
    
      <a rel="next" class="extend next" href="/2023/11/18/2023-11-18/">
        <span class="mdui-p-x-3" mdui-tooltip="{content: 'Android 10.0 SystemUI启动流程'}">下一篇</span>
        <button aria-label="next" class="mdui-btn mdui-btn-raised mdui-btn-icon mdui-color-theme-accent mdui-ripple"><i class="mdui-icon material-icons">arrow_forward</i></button>
      </a>
    
  </nav>





</main>
  <footer id="footer" class="mdui-m-t-5 mdui-p-y-3 mdui-color-theme">
  <div class="mdui-p-y-0 mdui-text-center">
    
    
    
    
    
    
    
      <a href="http://github.com/longzhiye" target="_blank" class="mdui-btn mdui-btn-icon mdui-text-color-theme-a100"><i class="mdui-icon iconfont">&#xe7ab;</i></a>
    
    
    
      <a href="https://www.zhihu.com/people/long-zhi-xie-61" target="_blank" class="mdui-btn mdui-btn-icon mdui-text-color-theme-a100"><i class="mdui-icon iconfont">&#xe6c0;</i></a>
    
    
    
      <a href="tencent://tencent:////message/?uin=951898105&Menu=yes" target="_blank" class="mdui-btn mdui-btn-icon mdui-text-color-theme-a100"><i class="mdui-icon iconfont">&#xe651;</i></a>
    
    
  </div>
  <div class="mdui-p-y-1 mdui-text-center">
    Copyright &copy; 2022 - 2025 龙之叶<br>
    Powered by <a href="https://hexo.io/" target="_blank" class="mdui-text-color-theme-accent">Hexo</a>
    <br>
      <span id="busuanzi_container_site_pv" style="display: none;"><i class="iconfont">&#xe7fd;</i> <span id="busuanzi_value_site_pv"></span></span> &nbsp;&nbsp;
      <span id="busuanzi_container_site_uv" style="display: none;"><i class="iconfont">&#xe601;</i> <span id="busuanzi_value_site_uv"></span></span>
    
  </div>
</footer>

  <button id="gotop" class="mdui-fab mdui-fab-fixed mdui-fab-hide mdui-color-theme-accent mdui-ripple"><i class="mdui-icon material-icons">arrow_upward</i></button>
  <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
  
<script src="/js/mdui.js"></script>
<script src="/js/script.js"></script>

  
<script src="/custom.js"></script>

</body>
</html>
